package com.njtoyo.taxi.admin.library.payment.cmb.sign;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {

    /**
     * MD5加密
     *
     * @param input
     * @return
     */
    public static String getMD5Content(String input) {
        try {
            // 拿到MD转换器
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");

            byte[] inputByteArray = input.getBytes();
            messageDigest.update(inputByteArray);

            byte[] resultByteArray = messageDigest.digest();

            return byteArrayToHex(resultByteArray);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将字节数组转换成16进组的字符串
     *
     * @param byteArray
     * @return
     */
    public static String byteArrayToHex(byte[] byteArray) {
        // 初始化一个字符数组，用来存放每个16进制字符
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

        char[] resultCharArray = new char[byteArray.length * 2];

        int index = 0;
        // 遍历字节数组，通过位运算，转换成字符放到字符数据中
        for (byte b : byteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }

        return new String(resultCharArray);
    }


    /**
     * 获取文件MD5
     *
     * @param path
     * @return
     */
    public static String getFileMD5(String path) {
        /**
         * if use BigInteger
         * real 00667580b5ea2559d5a9ea86aebaceed
         * but 667580b5ea2559d5a9ea86aebacaeed
         */
//        BigInteger bi = null;
        String strMd5 = null;
        try {
            byte[] buffer = new byte[8192];
            int len = 0;
            MessageDigest md = MessageDigest.getInstance("MD5");
            File f = new File(path);
            FileInputStream fis = new FileInputStream(f);
            while ((len = fis.read(buffer)) != -1) {
                md.update(buffer, 0, len);
            }
            fis.close();
            byte[] b = md.digest();
//            bi = new BigInteger(1, b);
            strMd5 = bytesToHexStr(b);
        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
        } catch (IOException e) {
//            e.printStackTrace();
        }
//        return bi == null ? null : bi.toString(16);
        return strMd5;
    }

    /**
     * 字节数组转十六进制字符串
     *
     * @param b 字节数组
     * @return 十六进制字符串
     */
    public static String bytesToHexStr(byte[] b) {
        StringBuilder strBuilder = new StringBuilder();
        String strTemp = "";
        for (int n = 0; n < b.length; ++n) {
            strTemp = (Integer.toHexString(b[n] & 0XFF));
            if (strTemp.length() == 1) {
                strBuilder.append("0").append(strTemp);
            } else {
                strBuilder.append(strTemp);
            }
        }
        return strBuilder.toString();
    }
}
